package com.dayuanit.space.map;

public class Map {

    public static final int x_max = 3;
    public static final int x_min = 0;
    public static final int y_max = 3;
    public static final int y_min = 0;


    //当前坐标
    private Point point;

    public Map() {
        this.point = new Point(0, 0);
    }

    public Map(int x, int y) {
        this.point = new Point(x, y);
    }


    public void forward(int step) {
        if (point.getY() + step > y_max) {
            return;
        }

        //在移动之前要先创建当前坐标的副本，也就是创建一个Point对象，将当前坐标的属性值赋值给副本对象。
        Point oldPoint = new Point(this.point.getX(), this.point.getY(), this.point.getPrePoint());

        point.changeY(step);
        point.setPrePoint(oldPoint);
    }

    public void back(int step) {
        if (point.getY() - step < y_min) {
            return;
        }

        Point oldPoint = new Point(this.point.getX(), this.point.getY(), this.point.getPrePoint());

        point.changeY(-step);
        point.setPrePoint(oldPoint);
    }

    public void left(int step) {
        if (point.getX() - step < x_min) {
            return;
        }

        Point oldPoint = new Point(this.point.getX(), this.point.getY(), this.point.getPrePoint());
        point.changeX(-step);
        point.setPrePoint(oldPoint);
    }

    public void right(int step) {
        if (point.getX() + step > x_max) {
            return;
        }

        Point oldPoint = new Point(this.point.getX(), this.point.getY(), this.point.getPrePoint());

        point.changeX(step);
        point.setPrePoint(oldPoint);
    }

    public void rollBack() {
        //如何实现回退功能
        //获取当前坐标的上一个坐标
        //如果当前坐标已经是原点了，那么此时获取的prePoint是null,就会出现空指针异常。
        Point oldPoint = this.point.getPrePoint();
        if (null == oldPoint) {
            System.out.println("已经到达原点，无法后退");
            return;
        }

        this.point = oldPoint;
    }

    public int getX() {
        return this.point.getX();
    }

    public int getY() {
        return this.point.getY();
    }
}
